//
//  BBArticleModel.m
//  BlockBrowser
//
//  Created by Mac on 2018/4/2.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBArticleModel.h"

@interface BBArticleModel()
@property(nonatomic, weak) NSTimer                     *timer;/**< timer */
@end
@implementation BBArticleModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{@"newsID":@"id"};
}
- (void)dealloc{
    [_timer invalidate];
    _timer = nil;
}
- (NSTimer*)timer{
    if (!_timer) {
//        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
    }
    return _timer;
}
- (BOOL)canCountDown{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [formatter setTimeZone:GTMzone];
    NSDate * dateLine = [formatter dateFromString:self.dateline];
    NSDate * date = [NSDate st_localCurrentDate];
    NSInteger secend = self.hours.integerValue * 60 * 60;
    NSInteger chazhi = date.timeIntervalSince1970 - dateLine.timeIntervalSince1970;
    if (chazhi > secend) {
        return NO;
    }
    return YES;
}
#pragma mark --Action Method
- (void)timerAction{

    NSString * time = self.dateline;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [formatter setTimeZone:GTMzone];
    NSDate * dateLine = [formatter dateFromString:self.dateline];
    NSDate * date = [NSDate st_localCurrentDate];
    NSInteger secend = self.hours.integerValue * 60 * 60;
    NSInteger chazhi = date.timeIntervalSince1970 - dateLine.timeIntervalSince1970;
    NSInteger count = secend - chazhi;
    if (self.canCountDown) {
        time = [self countDownWithSecend:count];
    }else{
        [_timer invalidate];
        _time = nil;
    }
    self.countTime = time;
    if (self.countDown) {
        self.countDown(time);
    }
}
- (void)timerBegain{

}
- (void)timerEnd{
    [_timer invalidate];
    _time = nil;
}
- (NSString*)countDownWithSecend:(NSInteger)secend{
    NSInteger hour = secend / 3600;
    NSString * hourStr = @(hour).description;
    if (hourStr.length < 2) {
        hourStr = [NSString stringWithFormat:@"0%@",@(hour)];
    }
    
    NSInteger minte = (secend - hour*3600) / 60;
    NSString * minteStr = @(minte).description;
    if (minteStr.length < 2) {
        minteStr = [NSString stringWithFormat:@"0%@",@(minte)];
    }
    
    NSInteger sec = secend % 60;
    NSString * secStr = @(sec).description;
    if (secStr.length < 2) {
        secStr = [NSString stringWithFormat:@"0%@",@(sec)];
    }
    return [NSString stringWithFormat:@"%@:%@:%@",hourStr,minteStr,secStr];
}
- (NSString *)currentDetailTime{
    NSString * time = self.dateline;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [formatter setTimeZone:GTMzone];
    NSDate * dateLine = [formatter dateFromString:self.dateline];
    NSDate * date = [NSDate st_localCurrentDate];
    NSInteger secend = self.hours.integerValue * 60 * 60;
    NSInteger chazhi = date.timeIntervalSince1970 - dateLine.timeIntervalSince1970;
    NSInteger count = secend - chazhi;
    return [self countDownWithSecend:count];
}
@end
